Continuous Integration
Part 2, Chapter 11
Next, we'll add continuous integration (CI), via GitHub Actions, to our project. We'll also set up GitHub Packages, a package management service, to store Docker images.
Do you usually use a different CI service or run Jenkins? It's encouraged to check your understanding by continuing to use the CI service that you're accustomed to. Convert the GitHub configuration examples as necessary.
GitHub Packages
GitHub Packages is a package management service, fully integrated with GitHub. It allows you to host your software packages, publicly or privately, for use within your projects on GitHub. We'll use it to store Docker images.
Assuming you have an account on GitHub, create a new repository for this project called fastapi-tdd-docker.
To test locally, you'll need to create a personal access token. Within your Developer Settings, click "Personal access tokens". Then, click "Generate new token". Provide a descriptive note and select the following scopes:
write:packagesread:packagesdelete:packagesworkflow
Take note of the token.
Build and tag the image:
$ docker build -f project/Dockerfile.prod -t docker.pkg.github.com/<USERNAME>/<REPOSITORY_NAME>/summarizer:latest ./project
# example:
# docker build -f project/Dockerfile -t docker.pkg.github.com/testdrivenio/fastapi-tdd-docker/summarizer:latest ./project
Next, using your personal access token, authenticate to GitHub Packages with Docker:
$ docker login docker.pkg.github.com -u <USERNAME> -p <TOKEN>
# example:
# docker login docker.pkg.github.com -u testdrivenio -p 3f18407a02445cb1837d0851f21a9757eccfdc8a
Push the image to the Container registry on GitHub Packages:
$ docker push docker.pkg.github.com/<USERNAME>/<REPOSITORY_NAME>/summarizer:latest
# example:
# docker push docker.pkg.github.com/testdrivenio/fastapi-tdd-docker/summarizer:latest
You should now be able to see the package at the following URL:
https://github.com/<USERNAME>/<REPOSITORY_NAME>/packages
GitHub Actions
To configure GitHub Actions, start by adding a new directory called ".github" in the root of your project. Within that directory add another directory called "workflows". Now, to configure a workflow, which is made up of one or more jobs, create a new file in the "workflows" directory called main.yml.
name: Continuous Integration and Delivery
on: [push]
env:
IMAGE: docker.pkg.github.com/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]')/summarizer
jobs:
build:
name: Build Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2.4.0
with:
ref: main
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Push image
run: |
docker push ${{ env.IMAGE }}:latest
test:
name: Test Docker Image
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout
uses: actions/checkout@v2.4.0
with:
ref: main
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Run container
run: |
docker run \
-d \
--name fastapi-tdd \
-e PORT=8765 \
-e ENVIRONMENT=dev \
-e DATABASE_URL=sqlite://sqlite.db \
-e DATABASE_TEST_URL=sqlite://sqlite.db \
-p 5003:8765 \
${{ env.IMAGE }}:latest
- name: Pytest
run: docker exec fastapi-tdd python -m pytest .
- name: Flake8
run: docker exec fastapi-tdd python -m flake8 .
- name: Black
run: docker exec fastapi-tdd python -m black . --check
- name: isort
run: docker exec fastapi-tdd python -m isort . --check-only
Here, after setting the global IMAGE environment variable, we defined two jobs, build and test.
In the build job, we:
- Check out the repository so the job has access to it
- Log in to GitHub Packages
- Pull the image if it exists
- Build the image
- Push the image up to GitHub Packages
Then, after building and running the image, in the test job, we run pytest, Flake8, Black, and isort.
Commit your changes, and then push to GitHub. This should trigger a new build, which should pass.
Once done, add a README.md file to the project root, adding the GitHub status badge:
# Test-Driven Development with FastAPI and Docker

Be sure to replace
YOUR_GITHUB_NAMESPACEwith your actual GitHub username or organization.
✓ Mark as Completed